#include "util.hpp"

void printProjectSet(std::vector<ProjectSet> const &res) {
  for (int i = 0; i < res.size(); ++i) {
    printf("=== project %d:\n", i);
    for (const auto &kv : res[i].itemKVs) {
      auto &pjItem = kv.second;
      printf("projectItem: %d %d\n", pjItem.defineExprIndex, pjItem.pointPos);
    }
    printf("convertion: ");
    for (const auto &kv : res[i].convertion) {
      printf("%s:%d ", kv.first.c_str(), kv.second);
    }
    printf("\n");
  }
}

void printTable(std::unordered_map<std::string, TableItem> &table) {
  for (const auto &kv : table) {
    printf("[%s]: ", kv.first.c_str());
    if (kv.second.isReduction) {
      printf("r%d\n", kv.second.num);
    } else {
      printf("%d\n", kv.second.num);
    }
  }
}
void printFollowSet(
    const std::unordered_map<std::string, std::unordered_set<std::string>>
        &followSet) {
  for (const auto &kv : followSet) {
    printf("%s: ", kv.first.c_str());
    for (const auto &item : kv.second) {
      printf("%s ", item.c_str());
    }
    printf("\n");
  }
}

std::vector<define>
DefineExpr2define(std::vector<DefineExpr> &defineExpressVec) {
  std::vector<define> res;
  for (const auto &item : defineExpressVec) {
    res.push_back(define{item.leftItem, item.rightItem, 0});
  }
  return res;
}

int is_all_digits(const char *str) {
  for (int i = 0; str[i]; i++) {
    if (!isdigit(str[i]))
      return 0;
  }
  return *str != '\0'; // 排除空字符串
}

int is_valid_ident(const char *str) {
  if (!isalpha(str[0]))
    return 0;
  for (int i = 1; str[i]; i++) {
    if (!isalnum(str[i]))
      return 0;
  }
  return 1;
}
